package com.mohe.zhihui.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.zhihui.dao.IDao;
import com.mohe.zhihui.model.Notice;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.INotice;
import com.mohe.zhihui.utils.Page;

public class NoticeImpl implements INotice {

	private IDao<Notice> noticeDao;

	public IDao<Notice> getNoticeDao() {
		return noticeDao;
	}

	public void setNoticeDao(IDao<Notice> noticeDao) {
		this.noticeDao = noticeDao;
	}

	@Override
	public List<Notice> getNoticeList(Notice notice, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Notice where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(notice.getTitle())){
			where.append(" and title like ?");
			params.add("%%"+notice.getTitle()+"%%");
		}
		where.append(" order by createDate desc");
		List<Notice> list = noticeDao.query(where.toString(), (page.getCurrPage()-1)*page.getPageSize(), page.getPageSize(), params);
		if(list!=null && list.size()>0){
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Notice notice) {
		StringBuffer where = new StringBuffer();
		where.append("from Notice where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(!StringUtils.isBlank(notice.getTitle())){
			where.append(" and title like ?");
			params.add("%%" + notice.getTitle() + "%%");
		}
		return noticeDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveNotice(Notice notice) {
		return noticeDao.insert(notice);
	}

	@Override
	public Notice getNoticeById(Long timeKey) {
		Notice notice = noticeDao.getByKey(timeKey);
		if(notice != null){
			return notice;
		}
		return null;
	}

	@Override
	public boolean updateNotice(Notice notice) {
		return noticeDao.update(notice);
	}

	@Override
	public boolean delNotice(Notice notice) {
		return noticeDao.delete(notice.getTimeKey());
	}

	@Override
	public List<Notice> getAllNotice() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return noticeDao.query(where.toString(), null);
	}

	

}
